

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.patterns.borg &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../index.html" />
    <link rel="up" title="pyamp.patterns" href="../patterns.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../patterns.html" accesskey="U">pyamp.patterns</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.patterns.borg</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The borg module contains the Borg class which provides an implementation</span>
<span class="sd">of the Borg design pattern, which is a Singleton like pattern for Python.</span>

<span class="sd">&#39;&#39;&#39;</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;Borg&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="Borg"><a class="viewcode-back" href="../../../_generated/pyamp_patterns_borg.html#pyamp.patterns.borg.Borg">[docs]</a><span class="k">class</span> <span class="nc">Borg</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The Borg class implements the Borg design pattern which provides</span>
<span class="sd">    a singleton like pattern for Python.</span>

<span class="sd">    A Borg object can be accessed by calling the :func:`getInstance` function.</span>
<span class="sd">    This functions returns an instance of the Borg class which stores its state</span>
<span class="sd">    between successive calls to get the Borg object.</span>

<span class="sd">    Instances of the Borg class can implement the :func:`init` function which</span>
<span class="sd">    is called when the Borg class is first created and initialized. This allows</span>
<span class="sd">    the concrete Borg classes to set up the initial state for their object.</span>

<span class="sd">    Example::</span>

<span class="sd">        class Example(Borg):</span>
<span class="sd">            def init(self, *args, **kwargs):</span>
<span class="sd">                self.x = kwargs[&#39;x&#39;]</span>

<span class="sd">        class First:</span>
<span class="sd">            def __init__(self):</span>
<span class="sd">                # An example of passing a value to the Borg instance</span>
<span class="sd">                b = Example(x=200)</span>
<span class="sd">                print &quot;First, b.x:&quot;, b.x</span>

<span class="sd">        class Second:</span>
<span class="sd">            def __init__(self):</span>
<span class="sd">                # Optionally could use Borg.getInstance()</span>
<span class="sd">                b = Example.getInstance()</span>
<span class="sd">                print &quot;Second, b.x:&quot;, b.x</span>
<span class="sd">                b.x = 500</span>

<span class="sd">        if __name__ == &#39;__main__&#39;:</span>
<span class="sd">            First()</span>
<span class="sd">            Second()</span>

<span class="sd">            b = Example.getInstance()</span>
<span class="sd">            print &quot;Third, b.x:&quot;, b.x</span>

<span class="sd">            # Prints:</span>
<span class="sd">            #    First, b.x: 200</span>
<span class="sd">            #    Second, b.x: 200</span>
<span class="sd">            #    Third, b.x: 500</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="n">__sharedState</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * args -- The arguments</span>
<span class="sd">        * kwargs -- The keyword arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__sharedState</span>

        <span class="c"># This needs to be moved one class up...</span>

        <span class="c"># Keep track of the initialization status of this class</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">getPrivateAttr</span><span class="p">(</span><span class="s">&quot;__init&quot;</span><span class="p">,</span> <span class="bp">False</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setPrivateAttr</span><span class="p">(</span><span class="s">&quot;__init&quot;</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">init</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

<div class="viewcode-block" id="Borg.init"><a class="viewcode-back" href="../../../_generated/pyamp_patterns_borg.html#pyamp.patterns.borg.Borg.init">[docs]</a>    <span class="k">def</span> <span class="nf">init</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;This function is called the first time the class is initialized,</span>
<span class="sd">        and should be overridden by concrete subclasses.</span>

<span class="sd">        * args -- The arguments</span>
<span class="sd">        * kwargs -- The keyword arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">pass</span>
</div>
<div class="viewcode-block" id="Borg.setPrivateAttr"><a class="viewcode-back" href="../../../_generated/pyamp_patterns_borg.html#pyamp.patterns.borg.Borg.setPrivateAttr">[docs]</a>    <span class="k">def</span> <span class="nf">setPrivateAttr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Set the value of a private class attribute.</span>

<span class="sd">        * name -- The name of the private attribute</span>
<span class="sd">        * value -- The value to set the attribute to</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__mangleName</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">value</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="Borg.getPrivateAttr"><a class="viewcode-back" href="../../../_generated/pyamp_patterns_borg.html#pyamp.patterns.borg.Borg.getPrivateAttr">[docs]</a>    <span class="k">def</span> <span class="nf">getPrivateAttr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Return the value of a private class attribute.</span>

<span class="sd">        * name -- The name of the private attribute</span>
<span class="sd">        * default -- The value returned if the attribute does not exist</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__mangleName</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">default</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">__mangleName</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Mangle the private attribute name.</span>

<span class="sd">        * name -- The private attribute name</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="s">&quot;_</span><span class="si">%s%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Borg.getInstance"><a class="viewcode-back" href="../../../_generated/pyamp_patterns_borg.html#pyamp.patterns.borg.Borg.getInstance">[docs]</a>    <span class="k">def</span> <span class="nf">getInstance</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get an instance of the Borg object.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="n">cls</span><span class="p">()</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../patterns.html" >pyamp.patterns</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>